#ifndef tmr_h
#define tmr_h

////////////////////////////////////////////////////
//
//	TIMER & OSCILLATOR MODULE
//
////////////////////////////////////////////////////


//-----------------Definitions-----------------------//	
	#define FOSC  8000000			// [VALUE]Oscillator Frequency
	/*#define TOSC  1/FOSC			// [VALUE]Oscillator Time Period
	#define RBPU 0b10000000 		// [MASK] PORTB Pull-Up Enable
	#define INTEDG 0b01000000 		// [MASK] Interrupt Edge Select
	#define TMR0CS 0b00100000 		// [MASK] TMR0 Clock Source Select	
	#define TMR0SE 0b00010000 		// [MASK] TMR0 Source Edge Select
	#define TMR0PSASSIGN 0b00001000		// [MASK] Timer0 Prescaler Assignment
	#define TMR0PSSELECT 0b00000111 	// [MASK] Timer0 Prescaler Rate Select
//	#define T1RUN 0b01000000		// [MASK] Timer1 System Clock Status
	#define TMR1CPS 0b00110000 		// [MASK] Timer1 Input Clock Prescale Select
	#define TMR1OSCEN 0b00001000		// [MASK] Timer1 Oscillator Enable Control	
	#define TMR1SYNC 0b00000100		// [MASK] Timer1 External Clock Input Sync
	#define TMR1CS 0b00000010		// [MASK] Timer1 Clock Source Select
	#define TMR2PS 0b01111000		// [MASK] Timer2 Output Postscale Select
	#define TMR2PRESEL 0b00000011		// [MASK] Timer2 Prescale Select*/
//-----------------Prototypes-----------------------//

// Timer0
	void Timer0_Init();
	void Timer0_IE();
	void Timer0_ID();
	void Timer0_OptionReg(int value);
// Timer1	
	void Timer1_Init();
	void Timer1_IE();
	void Timer1_ID();
	void Timer1_SetPeriod(unsigned short int H_Value, unsigned short int L_Value);
	void Timer1_Start();
	void Timer1_Stop();
	void Timer1_T1CON(int value);
// Timer2	
	void Timer2_Init();
	void Timer2_Start();
	void Timer2_Stop();
	void Timer2_Presc(unsigned short int preMode);
	void Timer2_IE();
	void Timer2_ID();
// Oscillator
	void  OSC_Osctune(int value);
	void  OSC_Osccon(int value);
 
//-----------C-Files--------------------------///
//void Timer0_Init()
// {
//	OPTION_REG |= RBPU;		// PORTB Pull-Up Enable
//	OPTION_REG |= INTEDG; 		// Interrupt Edge Select
//	OPTION_REG &= ~TMR0CS; 		// TMR0 Clock Source Select
//	OPTION_REG &= ~TMR0SE; 		// TMR0 Source Edge Select
//	OPTION_REG &= ~TMR0PSASSIGN; 	// Prescaler Assignment
// }

  void Timer0_IE()
 {
   INTCON.TMR0IE = 1; //Enables TMR0 Interrupt
 }
 
  void Timer0_ID()
 {
   INTCON.TMR0IE = 0; //Enables TMR0 Interrupt
 }
 
 
  void Timer0_OptionReg(unsigned short int value)
 {
    //Initial value on POR,BOR
    //OPTION_REG = 0xb11111111
    OPTION_REG = value;
 }

 void Timer1_Init()
 {
     //Clear TMR1CS bit
      //Clock source is internal clock (FOSC/4)
      T1CON  &= ~TMR1CS;

      //System clock Selector Bit
      T1CON.T1RUN =0;

      //Timer1 Prescaler : 8
      T1CON.T1CKPS1 =1;
     T1CON.T1CKPS0 =1;
      
      PIR1.TMR1IF = 0;            // clear TMR1IF
      TMR1H = 0;
	  TMR1L = 0;
     //TImer1 Interrupt enable
      Timer1_IE();
// 	//Oscillator Enable Control
//	T1CON |= TMR1CS;

//	// External Clock Input Sync
//			T1CON &= ~TMR1SYNC;

    /*
    T1CON =1;

    Timer1_IE(); */
 }
 
  void Timer1_IE()
 {//Preconditions: PIEI Bit in INTCON register is set (enabled)
   PIE1.TMR1IE = 1; //Enables TMR1 to PR1 Match Interrupt
 }

 void Timer1_ID()
 {
   PIE1.TMR1IE = 0; //Disables TMR1 Interrupt
 }
 
 void Timer1_SetPeriod(unsigned short int H_Value, unsigned short int L_Value)
 { 	// Stop the timer 
	// Change TRM1H & TMR1L values
	// Star the timer
    if(T1CON.TMR1ON =1)
    {
      Timer1_Stop();
      TMR1L = L_Value;
      TMR1H = H_Value;
      Timer1_Start();
    }
    else
       {
          Timer1_Stop();
          TMR1L = L_Value;
          TMR1H = H_Value;
       }

 }
  void Timer1_Start()
 {
		 PIR1.TMR1IF = 0;            // clear TMR1IF
        T1CON.TMR1ON =1;
 }

 void Timer1_Stop()
 {
        T1CON.TMR1ON =0;
 }
 
 void Timer1_T1CON(unsigned short int value)
 {  //Initial value on POR, BOR
    //TICON = 0xb-0000000
    T1CON = value;
 }
 

 void Timer2_Init()
 {


 }

 void Timer2_Start()
 {
  T2CON.TMR2ON =1;       //Start timer 2

 }
 void Timer2_Stop()
 {
  T2CON.TMR2ON =0;       //Stop timer 2

 }

 void Timer2_Presc(unsigned short int preMode)
 {
      switch(preMode)
      {
        case 0:
               T2CON &= ~0b00000011;                    //Prescaler [1]
               break;
        case 1:
              T2CON.T2CKPS0=1;                          //Prescaler [4]
              T2CON.T2CKPS1=0;
               break;
        case 2:
              T2CON.T2CKPS1 =1;                         //Prescaler [16]
               break;

      }
 }
 void Timer2_IE()
 {//Preconditions: PIEI Bit in INTCON register is set (enabled)
   PIE1.TMR2ON =1; //Enables TMR2 to PR2 Match Interrupt
 }

  void Timer2_ID()
 {
   PIE1.TMR2ON =0; //Disables TMR2 Interrupt
 }
 
 void OSC_Osctune(unsigned short int value)
 {
     OSCTUNE  = value;
 }
 void  OSC_Osccon(unsigned short int value)
 {
       OSCCON = value;
 }
 
// void Timer2_Postsc()
// {
//
// }

#endif